package com.yanqu.road.server.cmd.cross.activity;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eCrossUnionRankActivityType;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.ActivityProto;
import com.yanqu.road.pb.rank.CrossUnionActivityRankProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.UnionActivityMgr;
import com.yanqu.road.server.pb.ActivityPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_CROSS_ACTIVITY_UNION_CROSS_QUERY_MEMBER_CONDITION, desc = "跨服来查询跨服商会争霸成员贡献信息")
public class CrossUnionActivityQueryMemberConditionCmd extends AbstractCommand {

	private int activityId;

	private String unionUid;

	private long serverId;        //发起查询的区服编号

	private long userId;          //发起查询的玩家编号

	private UnionActivityConditionData unionActivityConditionData;

	private ActivityInfo activityInfo;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		CrossUnionActivityRankProto.CrossServerUnionActivityConditionUnionMemberDataReqMsg reqMsg = CrossUnionActivityRankProto.CrossServerUnionActivityConditionUnionMemberDataReqMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		unionUid = reqMsg.getUnionUid();
		serverId = reqMsg.getServerId();
		userId = reqMsg.getUserId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		activityInfo = NormalActivityMgr.getActivityInfo(activityId);
		if(null == activityInfo){
			ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
			return;
		}
		unionActivityConditionData = UnionActivityMgr.getUnionActivityConditionData(activityId, unionUid);
		if (null == unionActivityConditionData) {
			ret = GameErrorCode.E_ACTIVITY_NO_JOIN;
			return;
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		CrossUnionActivityRankProto.CrossServerUnionActivityConditionUnionMemberDataRespMsg.Builder respMsg = CrossUnionActivityRankProto.CrossServerUnionActivityConditionUnionMemberDataRespMsg.newBuilder();
		respMsg.setActivityId(unionActivityConditionData.getActivityId());
		respMsg.setRet(ret);
		respMsg.setServerId(serverId);
		respMsg.setUserId(userId);
		if (0 == ret) {
			if (null != unionActivityConditionData) {
				ActivityProto.UnionRankMemberConditionDataListMsg.Builder dataMsg = ActivityPb.parseUnionRankMemberConditionDataListMsg(
						UnionActivityMgr.getUserActivityConditionDataList(unionActivityConditionData, UnionActivityMgr.getUserActivityConditionDataMap(activityId)));
				respMsg.setDataList(dataMsg);
			}
		}
		//是否是跨服商会经验冲榜
		if (activityInfo.getType() == eActivityType.CrossUnionActivity.getValue()
				&& activityInfo.getChildType() == eCrossUnionRankActivityType.unionExpRiseRank.getIntValue()) {
			calcUnionTradeExpAdd(unionActivityConditionData.getValue().intValue(), respMsg);
		}
		YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_UNION_ACTIVITY_MEMBER_CONDITION_DATA_RESP, respMsg);
		GamePlayerMgr.sendPacket(0, pbMsg);
	}

	/**
	 * 计算商贸增加的经验
	 * @param unionExpAdd
	 * @param respMsg
	 */
	public void calcUnionTradeExpAdd(int unionExpAdd, CrossUnionActivityRankProto.CrossServerUnionActivityConditionUnionMemberDataRespMsg.Builder respMsg) {
		int unionTradeExpAdd;
		int unionMemberExpAdd = 0;
		ActivityProto.UnionRankMemberConditionDataListMsg dataList = respMsg.getDataList();
		for (ActivityProto.UnionRankMemberConditionDataTempMsg dataTempMsg : dataList.getDataListList()) {
			unionMemberExpAdd += Integer.parseInt(dataTempMsg.getValue());
		}
		unionTradeExpAdd = unionExpAdd - unionMemberExpAdd;
		if (unionTradeExpAdd < 0) {
			unionTradeExpAdd = 0;
		}
		respMsg.setParam(unionTradeExpAdd);
	}

}
